home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SpriteEngine / SE Hunter & Evader 2 / SpriteHanders.c < prev    next >
C/C++ Source or Header  |  1995-03-11  |  3KB  |  122 lines

  1. #include "SpriteTools.h"
  2.  
  3. // SpriteTools.h includes SpriteHandlers.h
  4.  
  5.  
  6. /*** Custom handlers - application dependent ***
  7.  
  8. Edit this as necessary. It should always include the following three routines:
  9.  
  10. MoveSprite: move the sprite
  11.  
  12. HitSprite: handle collisions between two sprites
  13.  
  14. InitSprites: Load all faces and create initial sprites
  15.  
  16. ***/
  17.  
  18.  
  19. GrafPtr    firstFace, secondFace, thirdFace;
  20. static    Point    gPlayerPosition;
  21.  
  22.  
  23. void MoveSprite(SpritePtr theSprite)
  24. {
  25.     Point    vector;
  26.     
  27.     switch (theSprite->kind)
  28.     {
  29.         case playerSprite:
  30.             GetMouse(&theSprite->position);
  31.             gPlayerPosition = theSprite->position;
  32.             KeepOnScreen(theSprite);
  33.             break;
  34.         case hunterSprite:
  35.  
  36.             theSprite->fixedPointPosition.h += theSprite->speed.h;
  37.             theSprite->fixedPointPosition.v += theSprite->speed.v;
  38.             theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
  39.             theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
  40.             KeepOnScreenFixed(theSprite);
  41.  
  42.             vector.h = gPlayerPosition.h - theSprite->position.h;
  43.             vector.v = gPlayerPosition.v - theSprite->position.v;
  44.             
  45.             theSprite->speed.h = vector.h / 8;
  46.             theSprite->speed.v = vector.v / 8;
  47.             
  48.             break;
  49.         case evaderSprite:
  50.  
  51.             theSprite->fixedPointPosition.h += theSprite->speed.h;
  52.             theSprite->fixedPointPosition.v += theSprite->speed.v;
  53.             theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
  54.             theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
  55.             KeepOnScreenFixed(theSprite);
  56.  
  57.             vector.h = gPlayerPosition.h - theSprite->position.h;
  58.             vector.v = gPlayerPosition.v - theSprite->position.v;
  59.  
  60.             theSprite->speed.h = -vector.h / 8;
  61.             theSprite->speed.v = -vector.v / 8;
  62.  
  63. /* Keep this sprite a bit inside the bounds so it doesn't just stand in a corner. */
  64.             if (theSprite->position.h < 50)
  65.             {
  66.                 theSprite->position.h = 50;
  67.                 theSprite->fixedPointPosition.h = theSprite->position.h << 4;
  68.             }
  69.             if (theSprite->position.h > gOffScreen->portRect.right - theSprite->face->portRect.right - 50)
  70.             {
  71.                 theSprite->position.h = gOffScreen->portRect.right - theSprite->face->portRect.right - 50;
  72.                 theSprite->fixedPointPosition.h = theSprite->position.h << 4;
  73.             }
  74.             if (theSprite->position.v < 50)
  75.             {
  76.                 theSprite->position.v = 50;
  77.                 theSprite->fixedPointPosition.v = theSprite->position.v << 4;
  78.             }
  79.             if (theSprite->position.v > gOffScreen->portRect.bottom - theSprite->face->portRect.bottom - 50)
  80.             {
  81.                 theSprite->position.v = gOffScreen->portRect.bottom - theSprite->face->portRect.bottom - 50;
  82.                 theSprite->fixedPointPosition.v = theSprite->position.v << 4;
  83.             }
  84.             break;
  85.     }
  86.  
  87. } /*MoveSprite*/
  88.  
  89.  
  90. void HitSprite(SpritePtr theSprite, SpritePtr anotherSprite)
  91. {
  92.     
  93. } /*HitSprite*/
  94.  
  95.  
  96.  
  97. void InitSprites()
  98. {
  99.     SpritePtr    theSprite;
  100.  
  101. /*Load all pictures*/
  102.     firstFace = LoadFaceFromCicn(128);            /*cicn resource #128.*/
  103.     secondFace = LoadFaceFromCicn(129);            /*cicn resource #129.*/
  104.     thirdFace = LoadFaceFromCicn(130);            /*cicn resource #130.*/
  105.  
  106. /*Create sprites*/
  107.     theSprite = NewSprite();
  108.     theSprite->kind = hunterSprite;
  109.     theSprite->face = firstFace;
  110.     SetPt(&theSprite->position, 100, 100);
  111.  
  112.     theSprite = NewSprite();
  113.     theSprite->kind = playerSprite;
  114.     theSprite->face = secondFace;
  115.     SetPt(&theSprite->position, 50, 50);
  116.  
  117.     theSprite = NewSprite();
  118.     theSprite->kind = evaderSprite;
  119.     theSprite->face = thirdFace;
  120.     SetPt(&theSprite->position, 150, 150);
  121. } /*InitSprites*/
  122.